<?php
/**
 * ClienteForm Registration
 * @author  <your name here>
 */
class ClienteForm extends TPage
{
    protected $form; // form
    
    /**
     * Class constructor
     * Creates the page and the registration form
     */
    function __construct()
    {
        parent::__construct();
        
        // creates the form
        $this->form = new TForm('form_Cliente');
        $this->form->class = 'tform'; // CSS class
        $this->form->style = 'width: 500px';
        
        // add a table inside form
        $table = new TTable;
        $table-> width = '100%';
        $this->form->add($table);
        
        $table->addRow()->addCell(new TXMLBreadCrumb('menu.xml','ClienteList'))->colspan = 2;
        // add a row for the form title
        $row = $table->addRow();
        $row->class = 'tformtitle'; // CSS class        
        $row->addCell( new TLabel('Cadastro de Cliente') )->colspan = 2;
        


        // create the form fields
        $id                             = new TEntry('id_cliente');
        $razao_social                   = new TEntry('razao_social');
        $apelido                        = new TEntry('apelido');
        $id_pais                        = new TDBCombo('id_pais', 'permission', 'Pais', 'id_pais', 'nome');        
        $num_ddd                        = new TEntry('num_ddd');
        $num_ddi                        = new TEntry('num_ddi');
        $telefone                       = new TEntry('telefone');


        // define the sizes
        $id->setSize(30);
        $id_pais->setSize(100);
        $razao_social->setSize(200);
        $apelido->setSize(200);
        $num_ddd->setSize(30);
        $num_ddi->setSize(30);
        $telefone->setSize(100);
        $telefone->setMask('9999999999');
        $num_ddd->setMask('999');
        $num_ddi->setMask('999');
        
        $id->setEditable(false);

        // validations
        $id_pais->addValidation('Pais', new TRequiredValidator);
        $razao_social->addValidation('Razão Social', new TRequiredValidator);
        $apelido->addValidation('Nome Fantasia', new TRequiredValidator);
        $num_ddd->addValidation('DDD', new TRequiredValidator);
        $num_ddi->addValidation('DDI', new TRequiredValidator);
        $telefone->addValidation('Telefone', new TRequiredValidator);        

        // add one row for each form field
        $table->addRowSet( $label_id = new TLabel('ID'), $id );
        $table->addRowSet( $label_razao_social = new TLabel('Razão Social (*)'), $razao_social );
        $table->addRowSet( $label_apelido = new TLabel('Nome Fantasia (*)'), $apelido );
        $table->addRowSet( $label_id_pais = new TLabel('Pais (*)'), $id_pais );
        $table->addRowSet( $label_num_ddd = new TLabel('DDD (*)'), $num_ddd );
        $table->addRowSet( $label_num_ddi = new TLabel('DDI (*)'), $num_ddi);
        $table->addRowSet( $label_telefone = new TLabel('Telefone (*)'), $telefone );

        $this->form->setFields(array($id, $razao_social,$apelido,$id_pais,$num_ddd,$num_ddi,$telefone));

        // create the form actions
        $save_button = TButton::create('save', array($this, 'onSave'), _t('Save'), 'ico_save.png');
        $new_button  = TButton::create('new',  array($this, 'onEdit'), _t('New'),  'ico_new.png');
        
        $list_button=new TButton('list');
        $list_button->setAction(new TAction(array('ClienteList','onReload')), _t('Back to the listing'));
        $list_button->setImage('ico_datagrid.png');
        $list_button->setImage('ico_datagrid.png');
        
        
        $this->form->addField($save_button);
        $this->form->addField($new_button);
        $this->form->addField($list_button);
        
        $buttons_box = new THBox;
        $buttons_box->add($save_button);
        $buttons_box->add($new_button);
        $buttons_box->add($list_button);
        
        
        // add a row for the form action
        $row = $table->addRow();
        $row->class = 'tformaction'; // CSS class
        $row->addCell($buttons_box)->colspan = 2;
        
        TScript::create('setTimeout(function() { $("input[name=\'razao_social\']").focus() }, 500);');
        
        parent::add($this->form);
    }

    /**
     * method onSave()
     * Executed whenever the user clicks at the save button
     */
    function onSave()
    {
        try
        {
            TTransaction::open('permission'); // open a transaction
            
            // get the form data into an active record Cliente
            $object = $this->form->getData('Cliente');
            $this->form->validate(); // form validation
            $object->store(); // stores the object
            $this->form->setData($object); // keep form data
            TTransaction::close(); // close the transaction
            
            // shows the success message
            new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
        }
        catch (Exception $e) // in case of exception
        {
            new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
            $this->form->setData( $this->form->getData() ); // keep form data
            TTransaction::rollback(); // undo all pending operations
        }
    }
    
    /**
     * method onEdit()
     * Executed whenever the user clicks at the edit button da datagrid
     */
    function onEdit($param)
    {
        try
        {
            if (isset($param['key']))
            {
                $key=$param['key'];  // get the parameter $key
                TTransaction::open('permission'); // open a transaction
                $object = new Cliente($key); // instantiates the Active Record
                $this->form->setData($object); // fill the form
                TTransaction::close(); // close the transaction
            }
            else
            {
                $this->form->clear();
            }
        }
        catch (Exception $e) // in case of exception
        {
            new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
            TTransaction::rollback(); // undo all pending operations
        }
    }
}
